/*

  Scaled mean of flats.

  Copyright © 2010-2011 F.Hroch (hroch@physics.muni.cz)

  This file is part of Munipack.

  Munipack is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  Munipack is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "munipack.h"
//#include <wx/cmdline.h>

/*
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cerrno>
#include <cstdlib>
*/


//using namespace std;


bool Munipack::mflat(MuniCmdLineParser& cmd)
{
  //  action = new MuniAction(this,"mflat",true);
  MuniProcess *action = new MuniProcess(&pipe,"mflat");
  pipe.push(action);

  wxString a;

  if( cmd.Found("n",&a) )
    action << ("Output level = " + a);

  if( cmd.Found("step",&a) )
    action << ("Grid step = " + a);

  //  action->AddCommonInput(cmd);
  //  action->CloseOutput();

  if( cmd.Found("o",&a) )
    action << ("Output filename = '" + a + "'");

  if( cmd.Found("b",&a) )
    action << ("Output BITPIX = " + a);

  action->Write(cmd.GetParams());

  pipe.Start();

  return true;
}


// int mflat(int argc, const char *argv[])
// {
//   // init, help
//   int arg0 = argc;
//   for(int i = 1; i < argc; i++) {

//     if( strcmp(argv[i],"mflat") == 0 ) 
//       arg0 = i + 1;

//     if( strcmp(argv[i],"--help") == 0 || arg0 == argc ) {
//       cout << "munipack mflat [-o file] [-b bitpix] [-n norm] [-step n] file(s)"
// 	   << endl;
//       return 0;
//     }

//   }

//   // open and load pipe
//   FILE *p;
//   if( (p = popen("mflat","w")) == NULL ) {
//     cerr << strerror(errno) << endl;
//     return errno;
//   }

//   fitskeys(p);

//   for(int i = arg0; i < argc; i++) {

//     if( strcmp(argv[i],"-o") == 0 && i++ < argc) 
//       fprintf(p,"Output filename = '%s'\n",argv[i]);

//     else if( strcmp(argv[i],"-b") == 0 && i++ < argc) 
//       fprintf(p,"Output BITPIX = %s\n",argv[i]);

//     else if( strcmp(argv[i],"-n") == 0 && i++ < argc) 
//       fprintf(p,"Output level = %s\n",argv[i]);

//     else if( strcmp(argv[i],"-step") == 0 && i++ < argc) 
//       fprintf(p,"Grid step = %s\n",argv[i]);

//     else if( strcmp(argv[i],"-") == 0 ) {
//       char line[32768];
//       while( true ) {
// 	cin.getline(line,32768);
// 	if( ! cin.good() ) break;
// 	fprintf(p,"'%s'\n",line);
//       }
//     }
    
//     else
//       fprintf(p,"'%s'\n",argv[i]);
//   }

//   pclose(p);

//   return errno;
// }
